﻿using System;
using System.Web;
using Enyim.Caching;
using Ayatta.Contract;
using System.Web.Mvc;
using Web.Base.Controllers;
using System.Configuration;
using Web.Base.ActionFilters;
using Web.Base.Components;
using Ayatta.Contract.Domain;
using Ayatta.Contract.Component;

namespace Web.My.Controllers
{
    [SkAuthorize(UserRole.Buyer)]
    public abstract class MyBaseController : BaseController
    {
        protected static readonly string AesKey = ConfigurationManager.AppSettings["Aes.Key"];
        protected static readonly string PasswordSalt = ConfigurationManager.AppSettings["Password.Salt"];

        protected MyBaseController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
            : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        [NonAction]
        protected SkResult<string> SaveAvatar(int userId, AvatarSize avatar, HttpPostedFileBase postedFile)
        {
            var fileName = string.Format("{0}.{1}.jpg", userId, avatar.GetSize());
            var result= FtpUpload(postedFile.InputStream, "/avatar/", fileName);
            if(result.Status)
            {
                if(avatar==AvatarSize.Big)
                {
                    PassportService.UpdateUserAvatar(userId, result.Data);
                }
            }
            return result;
        }

        [NonAction]
        protected SkResult<string> SaveAvatarLocal(int userId, AvatarSize avatar, HttpPostedFileBase postedFile)
        {
            var result = new SkResult<string>();
            var fileName = string.Format("/avatar/avatar.{0}.{1}.jpg", userId, avatar.GetSize());
            try
            {
                var file = WebSite.My + fileName;
                if (postedFile != null)
                {
                    postedFile.SaveAs(Server.MapPath(fileName));

                    result.Status = true;
                    result.Data = file;
                }
            }
            catch (Exception e)
            {
                result.Message = e.Message;
            }
            return result;
        }
    }
}
